package com.jc.common.util;

import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.commons.lang.StringUtils;
import org.springframework.cglib.beans.BeanMap;

import javax.servlet.http.HttpServletRequest;
import java.beans.PropertyDescriptor;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class WebUtils {

    //请求参数封装成map
    public static Map<String,Object> getParamAsDto(HttpServletRequest request){
        Map<String,Object> param = new HashMap<>();
        Map<String, String[]> map = request.getParameterMap();
        Iterator keyIterator = map.keySet().iterator();
        while(keyIterator.hasNext()) {
            String key = (String)keyIterator.next();
            String value = ((String[])map.get(key))[0];
            param.put(key.trim(), value.trim());
        }
        return param;
    }

//    public static Map<String,Object> ReadAsChars(HttpServletRequest request) throws UnsupportedEncodingException {
//        Map<String,Object> params = new HashMap<String, Object>();
//        BufferedReader br;
//        try {
//            br = request.getReader();
//            String str, wholeStr = "";
//            while((str = br.readLine()) != null){
//                wholeStr += str;
//            }
//            if(StringUtils.isNotEmpty(wholeStr)){
//                params = JSON.parseObject(wholeStr,Map.class);
//            }
//        } catch (IOException e1) {
//            e1.getLocalizedMessage();
//        }
//        return params;
//    }
    public static Map<String,Object> ReadAsChars(HttpServletRequest request) throws UnsupportedEncodingException {

        BufferedReader br = null;
        StringBuilder sb = new StringBuilder("");
        request.setCharacterEncoding("utf8");
        try
        {
            br = request.getReader();
            String str;
            while ((str = br.readLine()) != null)
            {
                sb.append(str);
            }
            br.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (null != br)
            {
                try
                {
                    br.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        if (StringUtils.isBlank(sb.toString())){
            return new HashMap<>();
        }
        String[] split = sb.toString().split("&");
        Map<String,Object> result = new HashMap<>();
        for (String val : split){
            if(val.split("=").length > 1){
                result.put(URLDecoder.decode(val.split("=")[0],"UTF-8"),URLDecoder.decode(val.split("=")[1],"UTF-8"));
            }
        }
        return result;
    }

    //Map转Bean
    public static <T> T mapToBean(Map<String, Object> map,T bean) {
        BeanMap beanMap = BeanMap.create(bean);
        beanMap.putAll(map);
        return bean;
    }
    //Bean转Map
    public static Map<String, Object> beanToMap(Object obj) {
        Map<String, Object> params = new HashMap<String, Object>(0);
        try {
            PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
            PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(obj);
            for (int i = 0; i < descriptors.length; i++) {
                String name = descriptors[i].getName();
                if (!"class".equals(name)) {
                    params.put(name, propertyUtilsBean.getNestedProperty(obj, name));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return params;
    }

    public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
        Map<String, Object> map = new HashMap<>();
        Class<?> clazz = obj.getClass();
        System.out.println(clazz);
        for (Field field : clazz.getDeclaredFields()) {
               field.setAccessible(true);
               String fieldName = field.getName();
                Object value = field.get(obj);
                map.put(fieldName, value);
        }return map;
    }

}
